package com.pmgames.store.data
{
	import com.pmgames.store.components.TextComponent;
	import com.pmgames.store.events.JavaFlexBoundaryConditionEvent;
	import com.pmgames.store.events.JavaFlexGameOverEvent;
	import com.pmgames.store.events.JavaFlexLetterClickedEvent;
	
	import flash.events.EventDispatcher;

	public class GuessedWord_Anagr extends EventDispatcher
	{
		private var wordToGuess:WordItem;
		private var guessedWord:ExtendedArrayCollection;
		private var lettersToOpen:int, tryNumber:int, stepsBeforeLose:int;
		
		public function setGuessedWord(value:String):void
		{
			for(var index:int = 0; index < value.length; index++)
			{
				guessedWord.addItem(value.charAt(index));
			}
		}
				
		public function getGuessedWord():String
		{
			return guessedWord.toString();
		}	
		
		public function setWordToGuess(value:WordItem):void
		{
			wordToGuess = value;
		}
				
		public function getWordToGuess():WordItem
		{
			return wordToGuess;
		}		
		
		public function setTryNumber(value:int):void
		{
			tryNumber = value;
		}	
		
		public function getTryNumber():int
		{
			return tryNumber;
		}		
		
		public function GuessedWord_Anagr(source:RelationArrayCollection = null, stepsBeforeLoseVal:int = -1):void
		{
			if(source != null)
			{
				var item:WordItem = RelationItem(source.getItemAt(source.length - 1)).sourceId;
				wordToGuess = new WordItem(
					item.id, 
					item.course, 
					item.definition, 
					item.image, 
					item.module, 
					item.name, 
					item.shortName, 
					item.type);
				tryNumber = 0;
				stepsBeforeLose = stepsBeforeLoseVal;	
				resetWord();
				
			}
		}
		
		public function addLetterToWord(letter:TextComponent, index:int):void
		{
			guessedWord.addItemAt(letter.text, index);
			lettersToOpen--;
			if(lettersToOpen < 0)
			{
				tryNumber++;	
				setSpecialSymbols();
				if(guessedWord.toString().toUpperCase() == wordToGuess.name.toUpperCase())
				{
					dispatchEvent(new JavaFlexGameOverEvent(JavaFlexGameOverEvent.WORD_GUESSED));
				}
				else
				{
					if(tryNumber >= stepsBeforeLose)
					{
						dispatchEvent(new JavaFlexGameOverEvent(JavaFlexGameOverEvent.WORD_NOT_GUESSED));
					}
					else
					{
						dispatchEvent(new JavaFlexBoundaryConditionEvent(JavaFlexBoundaryConditionEvent.BOUNDARY_CONDITION));
					}
				}
			}
			else
			{
				dispatchEvent(new JavaFlexLetterClickedEvent(JavaFlexLetterClickedEvent.LETTER_CLICKED, letter));	  
			}
		}
		
		private function setSpecialSymbols():void
		{
			for(var index:int = 0; index < wordToGuess.name.length; index++)
			{
				if(wordToGuess.name.charAt(index) < 'A' || wordToGuess.name.charAt(index) > 'z')
				{
					guessedWord.addItemAt(wordToGuess.name.charAt(index), index);
					lettersToOpen--;
				}
			}
		}
		
		public function resetWord():void
		{
			guessedWord = new ExtendedArrayCollection();
			guessedWord.createEmptyArray(wordToGuess.name.length - 1);
			setLettersToOpen();			
		}
		
		private function setLettersToOpen():void
		{
			lettersToOpen = wordToGuess.name.length - 1;
			for(var index:int = 0; index < wordToGuess.name.length; index++)
			{
				if(wordToGuess.name.charAt(index) < 'A' || wordToGuess.name.charAt(index) > 'z')
				{
					lettersToOpen--;
				}
			}
		}
		
	}
}